using UnityEngine;

public class ZombiePatrollerHealth : MonoBehaviour
{
    [Header("Health Settings")]
    [SerializeField] private int maxHealth = 50;
    [SerializeField] private int currentHealth;

    [Header("Visual Feedback")]
    [SerializeField] private float damageFlashDuration = 0.1f;

    private Renderer zombieRenderer;
    private Color originalColor;
    private ZombieDestroy zombieDestroy;

    private void Awake()
    {
        zombieRenderer = GetComponentInChildren<Renderer>();
        if (zombieRenderer == null)
        {
            zombieRenderer = GetComponent<Renderer>();
        }

        if (zombieRenderer != null)
        {
            originalColor = zombieRenderer.material.color;
        }

        zombieDestroy = GetComponent<ZombieDestroy>();
    }

    private void Start()
    {
        currentHealth = maxHealth;
    }

    public void TakeDamage(int damage)
    {
        currentHealth -= damage;

    
        if (zombieRenderer != null)
        {
            StartCoroutine(DamageFlash());
        }

       
        if (currentHealth <= 0)
        {
            Die();
        }
    }

    private System.Collections.IEnumerator DamageFlash()
    {
        if (zombieRenderer != null)
        {
            zombieRenderer.material.color = Color.red;
            yield return new WaitForSeconds(damageFlashDuration);
            zombieRenderer.material.color = originalColor;
        }
    }

    private void Die()
    {

        if (zombieDestroy != null)
        {
            zombieDestroy.DestroyZombie();
        }
        else
        {
            Destroy(gameObject);
        }
    }

    #region Getters
    public int GetCurrentHealth()
    {
        return currentHealth;
    }

    public int GetMaxHealth()
    {
        return maxHealth;
    }

    public bool IsDead()
    {
        return currentHealth <= 0;
    }
    #endregion
}